package com.qbw.app.module.vehicle.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.http.okhttp.HttpTask;
import com.qbw.actionmanager.ActionManager;
import com.qbw.app.Constant;
import com.qbw.app.R;
import com.qbw.app.action.VehicleSelectedAction;
import com.qbw.app.databinding.VehicleActivitySelectBinding;
import com.qbw.app.model.Vehicle;
import com.qbw.app.module.common.activity.CommonActivity;
import com.qbw.app.module.vehicle.adapter.VehicleSelectAdapter;
import com.qbw.app.module.vehicle.holder.VehicleSelectItemHolder;
import com.qbw.app.request.impl.VehicleRequest;

/**
 * @author QBW
 * @date 2019/10/8
 */
public class VehicleSelectActivity extends CommonActivity<VehicleActivitySelectBinding> implements VehicleSelectItemHolder.Listener {

    private VehicleSelectAdapter mAdapter;
    private Vehicle.VehicleList mVehicleList;

    public static void start(Context context, Vehicle.VehicleList vehicleList) {
        Intent intent = new Intent(context, VehicleSelectActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra(Constant.KEY_VEHICLE_LIST, vehicleList);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.vehicle_activity_select;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getIntent().getExtras() : savedInstanceState;
        mVehicleList = (Vehicle.VehicleList) args.getSerializable(Constant.KEY_VEHICLE_LIST);
        mBinding.title.setListener(this);
        mBinding.rcv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new VehicleSelectAdapter(this);
        mBinding.rcv.setAdapter(mAdapter);
        if (mVehicleList == null || mVehicleList.isEmpty()) {
            showLoadingDialog("");
            mGeneralManager.getRequestManager().getVehicleRequest().getVehicleList(this).execute();
        } else {
            mAdapter.addChild(mVehicleList.getList());
        }
    }

    @Override
    public void onLeftAreaClick() {
        onBackPressed();
    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        super.onHttpSuccess(httpTask, entity);
        String method = httpTask.getMethod();
        if (method.equals(VehicleRequest.METHOD_VEHICLE_LIST)) {
            Vehicle.Response response = (Vehicle.Response) entity;
            mVehicleList = response.getData();
            mAdapter.addChild(response.getData().getList());
            closeLoadingDialog();
        }
    }

    @Override
    public void onHttpFailed(HttpTask httpTask, int errorCode, String message) {
        super.onHttpFailed(httpTask, errorCode, message);
        String method = httpTask.getMethod();
        if (method.equals(VehicleRequest.METHOD_VEHICLE_LIST)) {
            closeLoadingDialog();
        }
    }

    @Override
    public void onBackPressed() {
        ActionManager.getInstance().triggerAction(new VehicleSelectedAction(mVehicleList));
        super.onBackPressed();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(Constant.KEY_VEHICLE_LIST, mVehicleList);
    }

    @Override
    public int onGetNowSelectedVehicleCount() {
        return mVehicleList == null ? 0 : mVehicleList.getSelectedVehicleList().size();
    }
}
